package com.qs.cwams.service.impl;

import com.qs.common.framework.util.DateTimeUtil;

import com.qs.cwams.mapper.AtteUserMapper;
import com.qs.cwams.pojo.domain.AtteUserDO;
import com.qs.cwams.pojo.view.AtteRecordVO;
import com.qs.cwams.service.ApiService;
import com.qs.cwams.service.AtteRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author suncheng
 */
@Service
public class ApiServiceImpl implements ApiService {
    private static final Logger logger = LoggerFactory.getLogger(ApiServiceImpl.class);

    /**
     * 指纹机初始化参数标识
     * 根据情况将以下配置加入初始化
     * 推送配置参数
     * PushOptionsFlag=0
     * PushOptions=RegDeviceType,FingerFunOn,FaceFunOn,FPVersion,FaceVersion,NetworkType,HardwareId3,HardwareId5,HardwareId56,LicenseStatus3,LicenseStatus5,LicenseStatus56
     */
    private static final String[] initKey = new String[] {"ATTLOGStamp", "OPERLOGStamp", "BIODATAStamp",
            "ATTPHOTOStamp", "ErrorDelay", "Delay", "TimeZone", "Realtime", "ServerVer", "PushProtVer",
            "SupportPing", "TransFlag"};

    @Autowired
    private AtteRecordService atteRecordService;
    @Autowired
    private AtteUserMapper atteUserMapper;
    private DateTimeUtil DateTimeUtil;

    private String defaultOptions(String sn) {
        StringBuilder sb = new StringBuilder("GET OPTION FROM: " + sn);
        sb.append("\n").append(initKey[0]).append("=").append("0");
        sb.append("\n").append(initKey[1]).append("=").append("0");
        sb.append("\n").append(initKey[2]).append("=").append("0");
        sb.append("\n").append(initKey[3]).append("=").append("0");
        sb.append("\n").append(initKey[4]).append("=").append("8");
        sb.append("\n").append(initKey[5]).append("=").append("2");
        sb.append("\n").append(initKey[6]).append("=").append("8");
        sb.append("\n").append(initKey[7]).append("=").append("1");
        sb.append("\n").append(initKey[8]).append("=").append("3.0.1");
        sb.append("\n").append(initKey[9]).append("=").append("2.4.1");
        sb.append("\n").append(initKey[10]).append("=").append("1");
        sb.append("\n").append(initKey[11]).append("=").append("111111111111");
        return sb.toString();
    }

    @Override
    public String init(String sn) {
        String options = defaultOptions(sn);
        logger.info(sn + " 初始化参数: \n" + options);
        return options;
    }

    @Override
    public void heart(String sn, String ip) {
        logger.info(String.format("获取指纹机%s心跳, IP地址%s", sn, ip));
    }

    @Override
    public void callback(String result, String sn) {
        String[] dataArray = result.split("\n");
        logger.info(String.format("指纹机设备 %s 命令执行结果数量: %s", sn, dataArray.length));
    }

    @Override
    public int attLogHandle(String data, String sn, String table) {
        String[] records = data.split("\n");
        for (String record : records) {
            String[] fields = record.split("\t");
            // 写入考勤记录
            Long userId = Long.valueOf(fields[0]);
            AtteUserDO atteUserDO = atteUserMapper.selectById(userId);
            if (atteUserDO != null) {
                Date date = DateTimeUtil.strToDate(fields[1], "yyyy-MM-dd HH:mm:ss");

                AtteRecordVO atteRecord = new AtteRecordVO();
                atteRecord.setUserId(atteUserDO.getUserId());
                atteRecord.setUserName(atteUserDO.getUserName());
                atteRecord.setNow(date);
                atteRecordService.record(atteRecord);
            }
        }
        return records.length;
    }
}
